<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use User\Api\UserApi;

/** 
 * 后台用户控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class ProductionController extends AdminController {

    /**
     * 采购 
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
  
    public function index(){
		 $map["id"]         =  (int)$_POST['id'];
    	 $map["productNo"]     = $_POST['productNo'];
    	 $map["goods_id"] = $_POST['goods_id'];
    	 $map["purchase_id"]    = $_POST['purchase_id'];
    	 $map["estimate_amount"]        = $_POST['estimate_amount'];
    	 $map["total_amount"]       = $_POST['total_amount'];
    	 $map["draw_amount"]        = $_POST['draw_amount'];
    	 $map["inferior_amount"]        = $_POST['inferior_amount'];
    	 $map["product_time"]        = $_POST['product_time'];  
        if(IS_POST){   
        	$res = M("manufacture")->where('id='. $map["id"])->save($map); 
        }	    	  
        $info = M()
            ->table('`one_manufacture` md INNER JOIN `one_purchase` m ON md.`purchase_id`= m.`id`')
            ->where('is_delete=0')
            ->field('md.*,m.status')
            ->order("status DESC")
            ->select();
         // print_r($info);die;   
       	foreach ($info as $key => $value) {
            $info[$key]['product_time']=date('Y-m-d',$value['product_time']); 
        }
        $this->assign('info',$info);
        $this->display();  
    }
    public function compile()
    {
    	$map['id'] = $_POST['ID'];  
    	$map['is_delete'] = 0; 
    	$list = M("manufacture")->where($map)->select(); 
    	$this->ajaxReturn($list); 
    }
 	public function deletes()
    {
    	// $map['id'] = $_GET['id'];  
    	$id = I('id'); 	
		$data['is_delete']=1; 
		$re = M('manufacture')->where(array('id'=>$id))->save($data);
        
		if($re == true){
			$this->success('删除成功');
		}
		else{
			$this->error('删除失败');
		} 
    } 

    // 投入生产
    public function GetRandStr($len)   
    {   
        $chars = array(   
             "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"   
        );   
        $charsLen = count($chars) - 1;   
        shuffle($chars);     
        $output = "";   
        for ($i=0; $i<$len; $i++)   
        {   
            $output .= $chars[mt_rand(0, $charsLen)];   
        }    
        return $output;    
    }      
    public function production_add(){ 
         $arr=$this->GetRandStr(4); 
         $data['purchase_id'] = $_GET['id']; 
         $data['productNo'] =   'SC'.date('YmdHis' ,time()).$arr;  //订单编号
         $data['goods_id'] = $data['goods_id']; 
         $data['estimate_amount'] = $data['estimate_amount']; 
         $data['total_amount'] = $data['total_amount']; 
         $data['draw_amount'] = $data['draw_amount']; 
         $data['inferior_amount'] = $data['inferior_amount']; 
         $data['product_time'] = time(); 
         $data['create_time'] = time();  
         $list['status'] = 2;  
         $res = M("manufacture")->add($data); 
         $r = M("purchase")->where('id='.$data['purchase_id'])->save($list); 
        if($res == true){
            $this->success('投入成功');
        }
        else{
            $this->error('投入失败');
        }   
    }

//登记角料
   public function registration($id){
   	   	  
   	  $status = M("manufacture")->find($id);
   	  $status["horns_status"] = 1;
   	  
   	  $is_status = M("manufacture")->save($status); 	  
   	   	  
   	  if($is_status){
   	  	$map["productNo"]    = $status["productNo"];
   	  	$map["purchase_id"]  = $status["purchase_id"];
   	  	$map["product_id"]   = $id;
   	  	$map["create_time"]  = time();
   	    M("Horns")->add($map); 
   	  	$this->success("登记成功");
   	  }
   }





}
